package com.fourth.dto.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * ClassName: PageResult
 * description: 分页结果
 *
 * @author wsy
 * @Date: 2022/11/20 12:16
 * @since JDK 1.8
 */
public class PageResult<T> extends ResultBase<PageResult.PageList<T>> {
    public PageResult() {
    }

    public static <T> PageResult<T> from(Page<T> page) {
        List<T> dataList = page.getRecords();
        //当前页索引
        long pageCurrent = page.getCurrent();
        //总条数
        long total = page.getTotal();
        PageList<T> pageList = new PageList();
        pageList.setPage((int) pageCurrent);
        pageList.setTotalCount((int) total);
        pageList.setList(dataList);
        PageResult<T> result = new PageResult();
        result.setData(pageList);
        return result;
    }


    public static class PageList<E> implements Serializable {
        @ApiModelProperty("页码")
        protected int page;
        @ApiModelProperty("总记录数")
        protected int totalCount;
        @ApiModelProperty("数据列表")
        protected List<E> list;

        public PageList() {
        }

        public void setPage(int page) {
            this.page = page;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public void setList(List<E> list) {
            this.list = list;
        }

        public int getPage() {
            return this.page;
        }

        public int getTotalCount() {
            return this.totalCount;
        }

        public List<E> getList() {
            return this.list;
        }

        public String toString() {
            return "PageResult.PageList(page=" + this.getPage() + ", totalCount=" + this.getTotalCount() + ", list=" + this.getList() + ")";
        }
    }
}
